package com.example.neteasecloudmusic.greenDaoGen;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.neteasecloudmusic.bean.SongDetailInfo;

import java.util.List;

public class SongDetailController {

    private DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private Context context;
    private SongDetailInfoDao songDetailInfoDao;
    private static volatile SongDetailController mSongDetailController;

    public static SongDetailController getInstance(Context context) {
        if (mSongDetailController == null) {
            synchronized (SongDetailController.class) {
                if (mSongDetailController == null) {
                    mSongDetailController = new SongDetailController(context);
                }
            }
        }
        return mSongDetailController;
    }

    /**
     * 初始化
     * @param context
     */
    public SongDetailController (Context context) {
        this.context = context;
        mHelper = new DaoMaster.DevOpenHelper(context, "songInfo.db", null);
        mDaoMaster = new DaoMaster(getWriteableDatabase());
        mDaoSession = mDaoMaster.newSession();
        songDetailInfoDao = mDaoSession.getSongDetailInfoDao();
    }

    private SQLiteDatabase getWriteableDatabase() {
        if(mHelper == null){
            mHelper =new DaoMaster.DevOpenHelper(context,"songInfo.db",null);
        }
        SQLiteDatabase db = mHelper.getWritableDatabase();
        return db;
    }


    /**
     * 获取可写数据库
     * @return
     */
    private SQLiteDatabase getReadableDatabase () {
        if (mHelper == null) {
            mHelper = new DaoMaster.DevOpenHelper(context, "songInfo.db", null);
        }
        SQLiteDatabase db = mHelper.getReadableDatabase();
        return db;
    }

    /**
     * 自动判定插入还是替换
     * @param songDetailInfo
     */
    public void  insertOrReplace (SongDetailInfo songDetailInfo) {
        songDetailInfoDao.insertOrReplace(songDetailInfo);
    }

    /**
     * 插入数据
     * @param songDetailInfo
     * @return
     */
    public long insert (SongDetailInfo songDetailInfo) {
        return songDetailInfoDao.insert(songDetailInfo);
    }

    /**
     * 更新数据
     * @param songDetailInfo
     */
    public void update (SongDetailInfo songDetailInfo) {
        SongDetailInfo mSongDetailInfo = songDetailInfoDao.queryBuilder()
                .where(SongDetailInfoDao.Properties.Id.eq(songDetailInfo.getId()))
                .build()
                .unique();
        if (mSongDetailInfo != null) {
            mSongDetailInfo.setSongName("xxx");
            songDetailInfoDao.update(mSongDetailInfo);
        }
    }

    /**
     * 按条件查询
     * @param wherecluse
     * @return
     */
    public List<SongDetailInfo> searchByWhere (String wherecluse) {
        List<SongDetailInfo> songDetailInfos = (List<SongDetailInfo>) songDetailInfoDao.queryBuilder().where(SongDetailInfoDao.Properties.SongName.eq(wherecluse)).build().unique();
        return songDetailInfos;
    }

    /**
     * 根据歌名和歌手名查询
     * @param song
     * @param singer
     * @return
     */
    public SongDetailInfo searchBySongAndSinger (String song, String singer) {
        SongDetailInfo songDetailInfo = songDetailInfoDao.queryBuilder().where(SongDetailInfoDao.Properties.SongName.eq(song))
                .where(SongDetailInfoDao.Properties.SingerName.eq(singer))
                .build()
                .unique();
        return songDetailInfo;
    }

    /**
     * 查询所有数据
     * @return
     */
    public List<SongDetailInfo> searchAll () {
        List<SongDetailInfo> songDetailInfos = songDetailInfoDao.queryBuilder().list();
        return songDetailInfos;
    }

    /**
     * 删除数据
     * @param id
     */
    public void deleteById (Long id) {
        songDetailInfoDao.queryBuilder().where(SongDetailInfoDao.Properties.Id.eq(id)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public void deleteAll () {
        songDetailInfoDao.deleteAll();
    }










}


















